home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / QD3D to QTVR / ArticleCode / Source / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-18  |  3.3 KB  |  157 lines  |  [TEXT/MPCC]

  1. #include "QD3DtoQTVR.h"
  2. #include "extern.h"
  3. #include "draw.h"
  4. #include "file.h"
  5. #include "myMovies.h"
  6. #include "lights.h"
  7.  
  8.  
  9. OSErr MyOpenFile(FSSpec *theFile, DocumentPtr theDocument)
  10.  
  11. {    OSErr                result;
  12.     short                refNum;
  13.     FInfo                fndrInfo ;
  14.     
  15.     FSpGetFInfo( theFile, &fndrInfo ) ;
  16.     
  17.     // Open file
  18.     if ((result = FSpOpenDF(theFile, fsRdWrPerm, &refNum)) != noErr)
  19.         return(result);
  20.     
  21.     // Assign file info to document
  22.     theDocument->fRefNum = refNum;
  23.     theDocument->theFileSpec = *theFile;
  24.     
  25.     // Set cursor and read model file
  26.     SetCursor(*GetCursor(watchCursor));
  27.     result = MyReadDocumentFile( theDocument);
  28.     
  29.     // Show window w/ title and reset cursor
  30.     SetWTitle((WindowPtr)theDocument->theWindow, theFile->name);
  31.     ShowWindow((WindowPtr)theDocument->theWindow);
  32.     SetCursor(&qd.arrow);
  33.     
  34.     return(result);
  35. }
  36.  
  37.  
  38. OSErr MyReadDocumentFile(DocumentPtr theDocument)
  39. {
  40.     TQ3StorageObject    storage;
  41.     TQ3FileObject        fd;
  42.     TQ3Object             objects = nil;
  43.     TQ3Status            status;
  44.     
  45.     // create new storage and file objects
  46.     storage = Q3MacintoshStorage_New( theDocument->fRefNum );
  47.     if (storage == nil)
  48.         goto bail;
  49.     fd = Q3File_New();
  50.     if (fd == nil)
  51.         goto bail;
  52.  
  53.     // associate the storage with the file
  54.     Q3File_SetStorage(fd, storage);
  55.     Q3Object_Dispose(storage);
  56.     
  57.     // read the drawable objects from the file object into a new group
  58.     status = MyReadScene(fd, theDocument);
  59.     if (status == kQ3Failure)
  60.         goto bail;
  61.     
  62.     Q3Object_Dispose(fd);
  63.  
  64.     return(noErr);
  65.     
  66. bail:
  67.     Q3Object_Dispose(fd);
  68.  
  69.     return(fnOpnErr);
  70. }
  71.  
  72. TQ3Status MyReadScene(TQ3FileObject file, DocumentPtr theDocument)
  73. {
  74.     TQ3Object            object = NULL;
  75.     TQ3Boolean            isEOF;
  76.     TQ3Boolean            isOpen;
  77.     TQ3Error            firstError;
  78.     TQ3ViewObject        view;
  79.     TQ3Object             model;
  80.     TQ3GroupObject         lightGroup = NULL;
  81.     
  82.     SetCursor(*GetCursor(watchCursor));
  83.  
  84.     // Create new model and get view                
  85.     model = Q3DisplayGroup_New();
  86.     if (!model)
  87.         goto bail;
  88.     theDocument->documentGroup = model;
  89.     view = theDocument->theView;
  90.     
  91.     // Open file for reading
  92.     if (Q3File_OpenRead(file, NULL) != kQ3Success)
  93.         goto bail;
  94.         
  95.     // Collect all drawable objects (into model) and any lights (into lightgroup)
  96.     while ((isEOF = Q3File_IsEndOfFile(file)) == kQ3False) {
  97.         object = Q3File_ReadObject(file);
  98.         
  99.         // if we got an error reading, then bail
  100.         if (Q3Error_Get(&firstError))
  101.             goto bail;
  102.         
  103.         // if object read is not NULL then process object
  104.         if (object != NULL) {
  105.             if (Q3Object_IsDrawable(object))
  106.                 Q3Group_AddObject(model, object);
  107.             
  108.             if (Q3Object_IsType(object, kQ3SharedTypeViewHints))
  109.                 if (view)
  110.                     Q3ViewHints_GetLightGroup((TQ3ViewHintsObject)object, &lightGroup);
  111.             
  112.             Q3Object_Dispose(object);
  113.         }
  114.     }
  115.     
  116.     // if didn't reach end of file properly, then bail
  117.     if (isEOF == kQ3False)
  118.         goto bail;
  119.  
  120.     // Add lights found to view if found.  Otherwise create default ones.
  121.     if (lightGroup) {
  122.         Q3View_SetLightGroup(view, lightGroup);
  123.         Q3Object_Dispose(lightGroup);
  124.     }
  125.     else
  126.         MyNewLights(theDocument);
  127.                 
  128.     Q3File_Close(file);
  129.     
  130.     SetCursor(&qd.arrow);
  131.     return kQ3Success;
  132.     
  133. bail:
  134.     Q3File_IsOpen(file, &isOpen);
  135.     if (isOpen == kQ3True)
  136.         Q3File_Close(file);
  137.  
  138.     if (object != NULL)
  139.         Q3Object_Dispose(object);
  140.         
  141.     if (model != NULL) {
  142.         Q3Object_Dispose(model);
  143.         model = theDocument->documentGroup = NULL;
  144.     }
  145.     
  146.     if (lightGroup != NULL)
  147.         Q3Object_Dispose(lightGroup);
  148.     
  149.     SetCursor(&qd.arrow);
  150.     
  151.     return kQ3Failure;
  152. }
  153.  
  154.  
  155.  
  156.  
  157.